#include "iniconfig.h"

#include <QDebug>
#include <QRegExp>

bool INIConfig::isIPValid(QString &ip)
{
    QRegExp regExp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");

    if(!regExp.exactMatch(ip))
    {
        return false;
    }
    return true;
}

bool INIConfig::safeVoluation(int &dest, int &src)
{
    if(0 > src)
    {
        return false;
    }
    dest = src;
    return true;
}

bool INIConfig::safeVoluation(int &dest, int &src, int min)
{
    if(min > src)
    {
        return false;
    }
    dest = src;
    return false;
}

bool INIConfig::safeVoluation(int &dest, int &src, int min, int max)
{
    if(min > src || max < src)
    {
        return false;
    }
    dest = src;
    return false;
}

bool INIConfig::safeVoluation(QString &dest, QString &src)
{
    if(src.isEmpty())
    {
        return false;
    }
    dest.clear();
    dest.append(src);
    return true;
}

bool INIConfig::safeVoluation(QString &dest, QString &src, int maxLength)
{
    if(src.isEmpty() || maxLength < src.length())
    {
        return false;
    }
    dest.clear();
    dest.append(src);
    return true;
}


INIConfig::INIConfig(QObject *parent) :
    QObject(parent)
{
}

INIConfig::~INIConfig()
{
    delete this->inifile;
}

bool INIConfig::getValue(int &arg, const char *key)
{
    arg = this->inifile->value(key).toInt();
    return true;
}

bool INIConfig::getValue(int &arg, const char *key, int min)
{
    arg = this->inifile->value(key).toInt();
    if(min > arg)
    {
        qCritical() << "INI 读取失败: 此Key不存在或不合法 ->" << key;
        return false;
    }
    return true;
}

bool INIConfig::getValue(int &arg, const char *key, int min, int max)
{
    arg = this->inifile->value(key).toInt();
    if(min > arg || max < arg)
    {
        qCritical() << "INI 读取失败: 此Key不存在或不合法 ->" << key;
        return false;
    }
    return true;
}

bool INIConfig::getValue(QString &arg, const char *key)
{
    arg.clear();
    arg = this->inifile->value(key).toString();
    if(arg.isEmpty())
    {
        qCritical() << "INI 读取失败: 此Key不存在或不合法 ->" << key;
        return false;
    }
    return true;
}

bool INIConfig::getValue(QString &arg, const char *key, int maxLength)
{
    arg.clear();
    arg = this->inifile->value(key).toString();
    if(arg.isEmpty() || maxLength < arg.length())
    {
        qCritical() << "INI 读取失败: 此Key不存在或不合法 ->" << key;
        return false;
    }
    return true;
}

bool INIConfig::getValue(QString &arg, const char *key, bool isIP)
{
    arg.clear();
    arg = this->inifile->value(key).toString();
    if(arg.isEmpty())
    {
        qCritical() << "INI 读取失败: 此Key不存在或不合法 ->" << key;
        return false;
    }
    if(isIP && !INIConfig::isIPValid(arg))
    {
        qCritical() << "INI 读取失败: 不合法的IP地址 -> " << key << " -> " << arg;
        return false;
    }
    return true;
}

bool INIConfig::initValue(const char *inipath)
{
    this->inifile = new QSettings(inipath, QSettings::IniFormat, 0);

    if(!this->getValue(this->myid, "Info/MyId", 0))
    {
        return false;
    }

    if(!this->getValue(this->codemaxlen, "Code/MaxLength", 3, 18))
    {
        return false;
    }

    if(!this->getValue(this->ifname, "Network/IFName", 32))
    {
        return false;
    }

    if(!this->getValue(this->staticip, "Network/StaticIP", true))
    {
        return false;
    }

    if(!this->getValue(this->staticnetmask, "Network/StaticNetmask", true))
    {
        return false;
    }

    if(!this->getValue(this->staticgatway, "Network/StaticGateway", true))
    {
        return false;
    }

    if(!this->getValue(this->dns1, "Network/DNS1", true))
    {
        return false;
    }

    if(!this->getValue(this->dns2, "Network/DNS2", true))
    {
        return false;
    }

    if(!this->getValue(this->serverip, "MySQL/ServerIP", true))
    {
        return false;
    }

    if(!this->getValue(this->serverport, "MySQL/ServerPort", 1, 65530))
    {
        return false;
    }

    if(!this->getValue(this->databasename, "MySQL/DatabaseName", 32))
    {
        return false;
    }

    if(!this->getValue(this->username, "MySQL/Username", 32))
    {
        return false;
    }

    if(!this->getValue(this->password, "MySQL/Password", 32))
    {
        return false;
    }

    if(!this->getValue(this->farmertable, "MySQL/FarmertTable", 32))
    {
        return false;
    }

    if(!this->getValue(this->vegetable, "MySQL/VegeTable", 32))
    {
        return false;
    }

    if(!this->getValue(this->logtable, "MySQL/LogTable", 32))
    {
        return false;
    }

    if(!this->getValue(this->datatable, "MySQL/DataTable", 32))
    {
        return false;
    }

    if(!this->getValue(this->synctimeout, "Time/SyncTimeout", 1800, 86400))
    {
        return false;
    }

    if(!this->getValue(this->serialpath, "Serial/Path", 128))
    {
        return false;
    }

    if(!this->getValue(this->serialrate, "Serial/BaudRate", 1200, 115200))
    {
        return false;
    }

    if(!this->getValue(this->serialdatabit, "Serial/DataBit", 5, 8))
    {
        return false;
    }

    if(!this->getValue(this->serialstopbit, "Serial/StopBit", 1, 2))
    {
        return false;
    }

    if(!this->getValue(this->serialseparate, "Serial/DataSeparate", 1))
    {
        return false;
    }

    if(!this->getValue(this->serialdatavalidlen, "Serial/DataValidLength", 3, 12))
    {
        return false;
    }

    if(!this->getValue(this->serialreadinterval, "Serial/ReadInterval", 0, 5))
    {
        return false;
    }

    if(!this->getValue(this->serialbufferlen, "Serial/BufferLength", 0, 255))
    {
        return false;
    }

    return true;
}

void INIConfig::dumpAll(void)
{
    qDebug() << "INI DUMP 数据\tifname -> " << this->ifname;
    qDebug() << "INI DUMP 数据\tcodemaxlen -> " << this->codemaxlen;
    qDebug() << "INI DUMP 数据\tstaticip -> " << this->staticip;
    qDebug() << "INI DUMP 数据\tstaticnetmask -> " << this->staticnetmask;
    qDebug() << "INI DUMP 数据\tstaticgatway -> " << this->staticgatway;
    qDebug() << "INI DUMP 数据\tdns1 -> " << this->dns1;
    qDebug() << "INI DUMP 数据\tdns2 -> " << this->dns2;

    qDebug() << "INI DUMP 数据\tserverip -> " << this->serverip;
    qDebug() << "INI DUMP 数据\tserverport -> " << this->serverport;
    qDebug() << "INI DUMP 数据\tdatabasename -> " << this->databasename;
    qDebug() << "INI DUMP 数据\tusername -> " << this->username;
    qDebug() << "INI DUMP 数据\tpassword -> " << this->password;

    qDebug() << "INI DUMP 数据\tfarmertable -> " << this->farmertable;
    qDebug() << "INI DUMP 数据\tvegetable -> " << this->vegetable;
    qDebug() << "INI DUMP 数据\tlogtable -> " << this->logtable;
    qDebug() << "INI DUMP 数据\tdatatable -> " << this->datatable;

    qDebug() << "INI DUMP 数据\tsynctimeout -> " << this->synctimeout;

    qDebug() << "INI DUMP 数据\tserialpath -> " << this->serialpath;
    qDebug() << "INI DUMP 数据\tserialrate -> " << this->serialrate;
    qDebug() << "INI DUMP 数据\tserialdatabit -> " << this->serialdatabit;
    qDebug() << "INI DUMP 数据\tserialstopbit -> " << this->serialstopbit;
    qDebug() << "INI DUMP 数据\tserialseparate -> " << this->serialseparate;
    qDebug() << "INI DUMP 数据\tserialdatavalidlen -> " << this->serialdatavalidlen;
    qDebug() << "INI DUMP 数据\tserialreadinterval -> " << this->serialreadinterval;
    qDebug() << "INI DUMP 数据\tserialbufferlen -> " << this->serialbufferlen;
}
